/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.setup.server.web;

import com.iwindplus.boot.web.controller.BaseController;
import com.iwindplus.setup.server.service.MailboxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 邮箱相关操作接口定义类.
 *
 * @author zengdegui
 * @since 2021/7/20
 */
@Tag(name = "邮箱接口")
@Slf4j
@RestController
@RequestMapping("mailbox")
@Validated
public class MailboxController extends BaseController {

    @Resource
    private MailboxService mailboxService;

    /**
     * 发送邮件.
     *
     * @param code        配置编码（必填）
     * @param subject     邮件标题（必填）
     * @param content     邮件内容（必填）
     * @param tos         收件人（必填）
     * @param ccs         抄送人（可选）
     * @param bccs        密送人（可选）
     * @param attachments 附件（可选）
     */
    @Operation(summary = "发送邮件")
    @PostMapping("send")
    public void send(
        @RequestParam String code,
        @RequestParam String subject,
        @RequestParam String content,
        @RequestParam List<String> tos,
        @RequestParam(required = false) List<String> ccs,
        @RequestParam(required = false) List<String> bccs,
        @RequestPart(required = false) List<MultipartFile> attachments) {
        this.mailboxService.send(code, subject, content, tos, ccs, bccs, attachments);
    }

    /**
     * 发送邮箱验证码（邮箱）.
     *
     * @param tplCode 模板配置编码（必填）
     * @param mail    邮箱（必填）
     */
    @Operation(summary = "发送邮箱验证码（邮箱）")
    @PostMapping("sendCaptcha")
    public void sendCaptcha(
        @RequestParam String tplCode,
        @RequestParam String mail) {
        this.mailboxService.sendCaptcha(tplCode, mail);
    }

    /**
     * 发送邮箱验证码（用户主键）.
     *
     * @param tplCode 模板配置编码（必填）
     */
    @Operation(summary = "发送邮箱验证码（用户主键）")
    @PostMapping("sendCaptchaByUserId")
    public void sendCaptchaByUserId(@RequestParam String tplCode) {
        final Long userId = this.getUserInfo().getUserId();
        final Long orgId = this.getUserInfo().getOrgId();
        this.mailboxService.sendCaptchaByUserId(tplCode, userId, orgId);
    }

}
